<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Model\OrderModel;

/**
 * 后台订单控制器
 * 
 * @author Ringo Chan<334258471@qq.com>
 */
class OrderController extends AdminController{
    
    /**
     * 商品订单列表
     *
     * @author Ringo Chan<334258471@qq.com>
     */
    public function index(){
        $username = I('username');
        $status = I('status');
        $Order = D('order');
        if ($username){
            $M_ucenter_member = M('ucenter_member');
            $members = $M_ucenter_member->where(array('id|username'=>array(intval($username),array('like','%'.$username.'%'),'_multi'=>true)))->select();
            
            $ids = array();
            foreach ($members as $member){
                $ids[] = $member['id'];
            }
            $map['uid'] = array('in', $ids);
        }
        $map['is_score_product'] = 0;
        switch ($status) {

            case '7':
                $map['pay_status'] = OrderModel::ORDER_APPLY_REFUND;
                break;
            case '8':
                $map['pay_status'] = OrderModel::ORDER_REFUND;
                break;
            case '0':
                $map['pay_status'] = OrderModel::ORDER_UNPAY;
                break;
            case '1':
                $map['order_status'] = OrderModel::ORDER_CONFIRMED;
                $map['shipping_status'] = array('in', array(OrderModel::SHIPPING_UNSHIPPED, OrderModel::SHIPPING_PARTIALSHIPMENT));
                $map['pay_status'] = OrderModel::ORDER_PAY;
                break;
            case '2':
                $map['order_status'] = OrderModel::ORDER_CONFIRMED;
                $map['shipping_status'] = OrderModel::SHIPPING_SHIPPED;
                break;
            case '3':
                $map['order_status'] = OrderModel::ORDER_CONFIRMED;
                $map['shipping_status'] = OrderModel::SHIPPING_SHIPPED;
                $map['review_status'] = OrderModel::REVIEW_UNREVIEWED;
                break;
            case '4':
                $map['order_status'] = OrderModel::ORDER_COMPLATED;
                $map['shipping_status'] = OrderModel::SHIPPING_SHIPPED;
                $map['review_status'] = OrderModel::REVIEW_REVIEWED;
                break;
            case '5':
                $map['order_status'] = OrderModel::ORDER_CANNELED;
                break;
            case '6':
                $map['order_status'] = array('in', array(OrderModel::ORDER_COMPLATED, OrderModel::ORDER_CONFIRMED));
                break;
        }

        $list   = $this->lists('Order', $map);
        foreach ($list as &$item) {
            $item['status_text'] = $Order->getOrderStatusText($item);
        }
        
        $this->assign('_list', $list);
        $this->meta_title = '商品订单列表';
        $this->display();
    }

    /**
     * 商品订单列表
     *
     * @author Ringo Chan<334258471@qq.com>
     */
    public function score_list(){
        $username = I('username');
        $status = I('status');
        $Order = D('order');
        if ($username){
            $M_ucenter_member = M('ucenter_member');
            $members = $M_ucenter_member->where(array('id|username'=>array(intval($username),array('like','%'.$username.'%'),'_multi'=>true)))->select();

            $ids = array();
            foreach ($members as $member){
                $ids[] = $member['id'];
            }
            $map['uid'] = array('in', $ids);
        }
        $map['is_score_product'] = 1;
        switch ($status) {

            case '7':
                $map['pay_status'] = OrderModel::ORDER_APPLY_REFUND;
                break;
            case '8':
                $map['pay_status'] = OrderModel::ORDER_REFUND;
                break;
            case '0':
                $map['pay_status'] = OrderModel::ORDER_UNPAY;
                break;
            case '1':
                $map['order_status'] = OrderModel::ORDER_CONFIRMED;
                $map['shipping_status'] = array('in', array(OrderModel::SHIPPING_UNSHIPPED, OrderModel::SHIPPING_PARTIALSHIPMENT));
                $map['pay_status'] = OrderModel::ORDER_PAY;
                break;
            case '2':
                $map['order_status'] = OrderModel::ORDER_CONFIRMED;
                $map['shipping_status'] = OrderModel::SHIPPING_SHIPPED;
                break;
            case '3':
                $map['order_status'] = OrderModel::ORDER_COMPLATED;
                $map['shipping_status'] = OrderModel::SHIPPING_SHIPPED;

                $map['review_status'] = OrderModel::REVIEW_REVIEWED;
                break;
            case '4':
                $map['order_status'] = OrderModel::ORDER_COMPLATED;
                $map['shipping_status'] = OrderModel::SHIPPING_SHIPPED;
                
                $map['review_status'] = OrderModel::REVIEW_UNREVIEWED;
                break;
            case '5':
                $map['order_status'] = OrderModel::ORDER_CANNELED;
                break;
            case '6':
                $map['order_status'] = array('in', array(OrderModel::ORDER_COMPLATED, OrderModel::ORDER_CONFIRMED));
                break;
        }

        $list   = $this->lists('Order', $map);
//        dump($list);
        foreach ($list as &$item) {

            $item['status_text'] = $Order->getOrderStatusText($item);
        }

        $this->assign('_list', $list);
        $this->meta_title = '积分商品订单列表';
        $this->display();
    }
    
    /**
     * 查看/编辑 商品订单
     * 
     * @param integer id 订单编号
     * @author Ringo Chan<334258471@qq.com>
     */
    public function edit(){
        $id = I('get.id');
        
        $M_order = M('Order');
        $info = $M_order->find($id);
        !$info && $this->error('非法的参数编号');
        $info['status_text'] = D('order')->getOrderStatusText($info);
        
        $this->assign('info', $info);
        $this->meta_title = '订单详情';
        $this->display();
    }
    
    /**
     * 发往买家
     * 
     * @param integer id 订单编号
     * @author Ringo Chan<334258471@qq.com>
     */
    public function shipping($id = 0 , $code = '', $shipping_code = ''){
        $M_order = M('Order');
        $M_shipping = M('shipping');
        $M_order_shipping = M('order_shipping');
        
        $info = $M_order->find($id);
        !$info && $this->error('非法的参数编号');
//        $info['status'] != 4 && $this->error('状态值错误，不能发快递');
        
        if (IS_POST) {
//            empty($shipping_code) && $this->error('请选择物流公司');
            $shipping_info = $M_shipping->where(array('code'=>$shipping_code))->find();
//            !$shipping_info && $this->error('请选择物流公司');
//            empty($code) && $this->error('快递单号不能为空');
            
            $order_shipping_id = $M_order_shipping->add(array('type'=>'admin', 'order_id'=>$id, 'code'=>$code, 'shipping_code'=>$shipping_code, 'create_time'=>NOW_TIME));
            
            if ($order_shipping_id > 0){
//                $M_order->where(array('id'=>$id))->setField('status', 5);
//                $M_order->where(array('id'=>$id))->setField('shipping_status', OrderModel::SHIPPING_PARTIALSHIPMENT);

                $data['order_status'] = OrderModel::ORDER_CONFIRMED;
                $data['shipping_status'] = OrderModel::SHIPPING_SHIPPED;
                $data['review_status'] = OrderModel::REVIEW_UNREVIEWED;

                $M_order->where(array('id'=>$id))->Save($data);
                $this->success('发往买家成功', U('Order/score_list'));
            } else {
                $this->error('发往失败，未知错误');
            }
        } else {
            $this->assign('info', $info);
            $this->meta_title = '发往买家';
            $this->display();
        } 
    }
    
    /**
     * 充值订单列表
     */
    public function payment(){
        $username = I('username');
        $status = I('status');
        
        $map['payment_code'] = array('neq', '');
        $map['method'] = '+';
        $map['status'] = array('egt', 0);
        
        if ($username){
            $M_ucenter_member = M('ucenter_member');
            $members = $M_ucenter_member->where(array('id|username'=>array(intval($username),array('like','%'.$username.'%'),'_multi'=>true)))->select();
        
            $ids = array();
            foreach ($members as $member){
                $ids[] = $member['id'];
            }
            $map['uid'] = array('in', $ids);
        }
        
        $status && $map['status'] = $status;
        
        $list   = $this->lists('payment_log', $map, 'create_time DESC');
        int_to_string($list, array('status'=>getStatus('payment')));
        
        $this->assign('_list', $list);
        $this->meta_title = '充值订单列表';
        $this->display();
    }
    
    /**
     * 通用改变状态
     *
     * @param string $method 方法名
     * @param number $status 状态值
     * @param array id 编号
     * @author Ringo Chan <334258471@qq.com>
     */
    public function changeStatus($method=null, $status=0){
        $map['id'] =   array('in',I('id', 0));
        
        $data['status'] = $status;
        $data['update_time'] = NOW_TIME;
        
        $feedback = false;

        switch ( strtolower($method) ){
            case 'order':
                $M_order = M('order');
                $feedback = $M_order->where($map)->save($data);
                break;
            default:
                $this->error('参数非法');
        }
    
        $feedback !== false ? $this->success('状态修改成功！') : $this->error('状态修改失败！');
    }
}
?>